<?php
/**
 * @package panel
 * @subpackage panel.environment.loggers
 */
/**
 * Logger for errors
 * @author Dmitry Bakaleinik (code)
 */
class panel__logger__errors
	implements maf__i__singletone {

	/**
	 * @var maf__fso__file
	 */
	protected $file;

	/**
	 * @return panel__logger__errors
	 */
	static public function getInstance() {

		static $instance;

		if( !isset($instance) ) {
			$instance = new self; }

		return $instance;

	}	# Function

	protected function __clone() {
	}	# Function

	protected function __sleep() {
	}	# Function

	protected function __wakeup() {
	}	# Function

	protected function __construct() {

		$this->file = new maf__fso__file(PANEL__PATH__LOGS . "/error.log");

	}	# Function

	protected function log($_type_, $_message_) {

		$trace = debug_backtrace();

		$message = <<<MESSAGE
-------------------------------------------------------------------------------
Type: %s
Time: %s

Logged from:
	%s%s%s()
	%s:%s

Message:
%s


MESSAGE;

		$message = sprintf(
			$message,
			(string) $_type_,
			date("Y-m-d H:i:s"),
			isset_or($trace[2]["class"], ""),
			isset_or($trace[2]["type"],  ""),
			isset_or($trace[2]["function"], "{main}"),
			isset_or($trace[1]["file"],  ""),
			isset_or($trace[1]["line"],  ""),
			(string) $_message_
		);

		$this->file->appendContents(html_entity_decode($message));

		panel__logger__mailer::getInstance()->log("ERROR REPORT", $message);

	}	# Function

	public function logException( exception $_ ) {

		$this->log("Exception", $_->__toString());

	}	# Function

	public function logDump($_full_ = TRUE) {

		$dump = ($_full_) ?
			var_export(
				array(
					"REQUEST"    => isset_or($_REQUEST),
					"SERVER"     => isset_or($_SERVER),
					"USAGE"      => function_exists("memory_get_usage") ? (memory_get_usage(TRUE) / 1024) . "kb" : "",
					"PEAK USAGE" => function_exists("memory_get_peak_usage") ? (memory_get_peak_usage(TRUE) / 1024) . "kb" : ""
				),
				TRUE
			) :
			var_export(
				array(
					"REQUEST"    => isset_or($_REQUEST),
					"USAGE"      => function_exists("memory_get_usage") ? (memory_get_usage(TRUE) / 1024) . "kb" : "",
					"PEAK USAGE" => function_exists("memory_get_peak_usage") ? (memory_get_peak_usage(TRUE) / 1024) . "kb" : ""
				),
				TRUE
			)
		;

		$this->log("Dump", $dump);

	}	# Function

}	# Class

/* EOF */